package com.homni.iul.util;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class TransformUtil {

	// 0-255转16位byte
	public static byte intTo16String(int value) {
		byte devBin = 0;
		
		if (value < 256){
			//整型转16进制：　　
			String hexString = Integer.toHexString(value);
			//16进制转为字节：
			devBin = (byte) Integer.parseInt(hexString, 16);
		}
		
		return devBin;
	}

	/**
	 * long 转 byte[] 例如 156转成{0，0，0，0，0，0，0，-100}
	 * 
	 */
	public static byte[] long2Bytes(long num) {

		byte[] byteNum = new byte[8];
		for (int ix = 0; ix < 8; ++ix) {
			int offset = 64 - (ix + 1) * 8;
			byteNum[ix] = (byte) ((num >> offset) & 0xff);
		}

		return byteNum;
	}

	/**
	 * long 转 byte[] 例如 156转成{-100，0，0，0，0，0，0，0}
	 * 
	 */
	public static byte[] longToBytes2(long value) {
		byte[] src = new byte[8];
		src[7] = (byte) ((value >> 56) & 0xFF);
		src[6] = (byte) ((value >> 48) & 0xFF);
		src[5] = (byte) ((value >> 40) & 0xFF);
		src[4] = (byte) ((value >> 32) & 0xFF);
		src[3] = (byte) ((value >> 24) & 0xFF);
		src[2] = (byte) ((value >> 16) & 0xFF);
		src[1] = (byte) ((value >> 8) & 0xFF);
		src[0] = (byte) (value & 0xFF);
		return src;
	}

	/**
	 * byte[]转long 例如 {-100，0，0，0，0，0，0，0}转成156
	 * 
	 */
	public static long bytesTolong(byte[] src, long offset) {
		long value;
		value = (long) ((src[(int) offset] & 0xFF) | ((src[(int) (offset + 1)] & 0xFF) << 8)
				| ((src[(int) (offset + 2)] & 0xFF) << 16) | ((src[(int) (offset + 3)] & 0xFF) << 24)
				| ((src[(int) (offset + 4)] & 0xFF) << 32) | ((src[(int) (offset + 5)] & 0xFF) << 40)
				| ((src[(int) (offset + 6)] & 0xFF) << 48) | ((src[(int) (offset + 7)] & 0xFF) << 56));
		return value;
	}

	/**
	 * byte[]转long
	 * 
	 */
	public static long bytes2Long(byte[] byteNum) {
		long num = 0;
		for (int ix = 0; ix < 8; ++ix) {
			num <<= 8;
			num |= (byteNum[ix] & 0xff);
		}
		return num;
	}

	/**
	 * byte[]转字符串
	 * 
	 */
	public static String ArrayTo16String(byte[] data) {

		StringBuilder result = new StringBuilder();

		for (byte b : data) {

			result.append(String.format("%02X", b));

		}
		return result.toString();

	}

	public static String byteToHexString(byte[] b) {
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			hexString.append(hex.toUpperCase());
		}
		return hexString.toString();
	}

	public static byte byteTo10Positivebyte(byte b) {
		byte result = (byte) (b & 0xff);
		return result;

	}

	/**
	 * int转byte[] 例如156转{0，0，0，-100}
	 */
	public static byte[] int2Bytes(int num) {
		byte[] byteNum = new byte[4];
		for (int ix = 0; ix < 4; ++ix) {
			int offset = 32 - (ix + 1) * 8;
			byteNum[ix] = (byte) ((num >> offset) & 0xff);
		}
		return byteNum;
	}

	/**
	 * byte[]转int 例如 {-100，0，0，0}转156
	 */
	public static int bytesToInt(byte[] src, int offset) {
		int value;
		value = (int) ((src[offset] & 0xFF) | ((src[offset + 1] & 0xFF) << 8) | ((src[offset + 2] & 0xFF) << 16) | ((src[offset + 3] & 0xFF) << 24));
		return value;
	}

	/**
	 * byte[]转int 例如 {0，0，0，-100}转156
	 */
	public static int bytesToInt2(byte[] src, int offset) {
		int value;
		value = (int) (((src[offset] & 0xFF) << 24) | ((src[offset + 1] & 0xFF) << 16)
				| ((src[offset + 2] & 0xFF) << 8) | (src[offset + 3] & 0xFF));
		return value;
	}

	/**
	 * int转byte[] 例如156转{-100，0，0，0，}
	 */
	public static byte[] intToBytes(int value) {
		byte[] src = new byte[4];
		src[3] = (byte) ((value >> 24) & 0xFF);
		src[2] = (byte) ((value >> 16) & 0xFF);
		src[1] = (byte) ((value >> 8) & 0xFF);
		src[0] = (byte) (value & 0xFF);
		return src;
	}

	public static byte[] intToBytes2(int value) {
		byte[] src = new byte[4];
		src[0] = (byte) ((value >> 24) & 0xFF);
		src[1] = (byte) ((value >> 16) & 0xFF);
		src[2] = (byte) ((value >> 8) & 0xFF);
		src[3] = (byte) (value & 0xFF);
		return src;
	}

	public static byte[] intTo4bytes(int number) {
		byte[] bytes = new byte[4];
		String hexString = Integer.toHexString(number);
		StringBuffer sb = new StringBuffer();
		sb.append(hexString);
		while (true) {
			if (sb.length() == 8) {
				break;
			}
			sb.insert(0, "0");
		}

		// System.out.println(number+"="+sb.toString());
		for (int i = 0; i < 4; i++) {
			bytes[3 - i] = (byte) Integer.parseInt(sb.toString().substring(i * 2, i * 2 + 2), 16);
			// System.out.println("bytes["+(3-i)+"]=" + bytes[3-i]);
		}
		return bytes;
	}

	public static byte[] shortToByteArray(short s) {
		byte[] shortBuf = new byte[2];
		for (int i = 0; i < 2; i++) {
			int offset = (shortBuf.length - 1 - i) * 8;
			shortBuf[i] = (byte) ((s >>> offset) & 0xff);
		}
		return shortBuf;
	}

	public static byte[] StringToBytes(String src) {
		byte[] bytes = null;
		String stringtemp = src;
		try {

			bytes = stringtemp.getBytes("UTF-8");

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return bytes;
	}

	//字节数组转UUID数组
	public static List<UUID> parseUuids(byte[] advertisedData) {
		List<UUID> uuids = new ArrayList<UUID>();

		ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(ByteOrder.LITTLE_ENDIAN);
		while (buffer.remaining() > 2) {
			byte length = buffer.get();
			if (length == 0)
				break;

			byte type = buffer.get();
			switch (type) {
				case 0x02: // Partial list of 16-bit UUIDs
				case 0x03: // Complete list of 16-bit UUIDs
					while (length >= 2) {
						uuids.add(UUID.fromString(String.format("%08x-0000-1000-8000-00805f9b34fb", buffer.getShort())));
						length -= 2;
					}
					break;

				case 0x06: // Partial list of 128-bit UUIDs
				case 0x07: // Complete list of 128-bit UUIDs
					while (length >= 16) {
						long lsb = buffer.getLong();
						long msb = buffer.getLong();
						uuids.add(new UUID(msb, lsb));
						length -= 16;
					}
					break;

				default:
					buffer.position(buffer.position() + length - 1);
					break;
			}
		}

		return uuids;
	}

}
